//
//  GLDisplayobject.m
//  BallGame
//
//  Created by 沈 冬冬 on 12-1-15.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "GLDisplayobject.h"
#import "BGSceneController.h"
#import "Constants.h"
#import "GLDisplayobject.h"
#import "ColorDefine.h"
#import "PostionFunc.h"

@implementation GLDisplayobject

//@synthesize x,y,z,parent;
@synthesize realPos,parent;
@synthesize destion,speed,rotationalSpeed,isPaused,isMoving,duration,tweenEndCallback;

-(id) init
{
	self = [super init];
	if (self) {
		mouseEnable = NO;
		self.displayBackGroundColor = blackColor;
	}
	return self;
}

-(void) setPos:(BGPoint) posValue
{
	pos = posValue;
	[self adjustPos];
}

-(BGPoint) pos
{
	return pos;
}

-(void) setParent:(GLDisplayobject *)parentCon
{
	parent = parentCon;
	[self adjustPos];
}

-(void) adjustPos			//调整坐标
{
	if (parent) {
		self.realPos = BGPointMake(parent.realPos.x + pos.x, parent.realPos.y + pos.y, parent.realPos.z + pos.z);
	}
	else {
		self.realPos = BGPointMake(pos.x, pos.y, pos.z);
	}
	[self awake];
}

-(void) setDisplayBackGroundColor:(int) color
{
	displayBackGroundColor = color;
	if (color == redColor) 
	{
		normalColor = GLColorsRed;
		pressedColor = GLColorsRedPressed;
	}
	else if (color == greenColor) 
	{
		normalColor = GLColorsGreen;
		pressedColor = GLColorsGreenPressed;
	}
	else if (color == blueCell) 
	{
		normalColor = GLColorsBlue;
		pressedColor = GLColorsBluePressed;
	}
	else if (color == yellowCell) 
	{
		normalColor = GLColorsYellow;
		pressedColor = GLColorsYellowPressed;
	}
	else if (color == orangeCell) 
	{
		normalColor = GLColorsOrange;
		pressedColor = GLColorsOrangePressed;
	}
	else if (color == whiteColor) 
	{
		normalColor = GLColorWhite;
		pressedColor = GLColorWhitePressed;
	}
	else {
		normalColor = GLColorBlack;
		pressedColor = GLColorBlackPressed;
	}
}

-(void)update
{
	if (isMoving) 
	{
		if(!isPaused)
		{
			CGFloat deltaTime = [[BGSceneController sharedSceneController] deltaTime] * 1000;
			
			self.pos = BGPointMake(speed.x*deltaTime + pos.x, speed.y*deltaTime+ pos.y, speed.z * deltaTime+pos.z);
			
			translation = [PostionFunc turnPositonToScenePositon:self];	
			
			rotation.x += rotationalSpeed.x * deltaTime;
			rotation.y += rotationalSpeed.y * deltaTime;
			rotation.z += rotationalSpeed.z * deltaTime;
			
			[self checkMoveFinished];			
		}
	}
	[super update];
}

-(void) render
{
	if (visible)			//只有visibel为true的时刷新
	{
		[super render];
	}
}

-(void) awake
{
	translation = [PostionFunc turnPositonToScenePositon:self];	

	mesh.colors = normalColor;
	
	[super awake];
}

-(void) checkMoveFinished			//查看是否已经move结束
{
	curUpdatedCount++;
	if (curUpdatedCount >= needUpdateCount) 
	{
		[self stopMove];
	}
}

-(void) stopMove
{
	isMoving = NO;
	isPaused = NO;
	curUpdatedCount = 0;
	needUpdateCount = 0;
	[tweenEndCallback release];
	return;
}

-(void) jumpToEnd:(BOOL)needCallFunc			//跳转到运行结束
{
	translation.x = destion.x;
	translation.y = destion.y;
	translation.z = destion.z;
	if (needCallFunc) {
		[self MoveEndHandler];
	}
	[self stopMove];
}

-(void) startMove				//开始移动，计算速度
{
	if (duration <= 0) 
	{
		return;
	}
	
	GLfloat speedx = (destion.x - self.pos.x) / duration;
	GLfloat speedy = (destion.y - self.pos.y) / duration;
	GLfloat speedz = (destion.z - self.pos.z) / duration;
	
	speed = BGPointMake(speedx, speedy, speedz);
	
	isMoving = YES;
	isPaused = NO;
	
	curUpdatedCount = 0;
	needUpdateCount = duration / frameInterval;				//初始化需要update的次数
	
}

-(void) pauseMove
{
	isPaused = YES;
}

-(void) resumeMove
{
	isPaused = NO;
}

-(void) MoveEndHandler
{
	if(tweenEndCallback)
		NSLog(@"Move End");
}

-(void) dealloc
{
	[super dealloc];
}

@end
